<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

	public $sidebar_content='';

	function __construct()
	{
		parent::__construct();

		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('ion_auth');
		$this->load->database();
		//$this->load->library('session');

	}

	function index($id='')
	{
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login','refresh');
		}

		$status = 'add';
		$data['name'] = '';
		$data['id'] = '';
		$data['order'] = '';

		$id = intval($id);
		if($id == '')
		{
			$id = $this->input->post('id');
			$data['id'] = $id;

		}
		if($id != '')
		{
			$query = $this->db->from('main')->where('id',$id)->limit(1)->get();
			if($query->row())
			{
				$status = 'edit';
				foreach($query->result() as $row)
				{
					$data['id'] = $row->id;
					$data['name'] = $row->name;
					$data['order'] = $row->order;
				}
			}

			$data['legend'] = '编辑分类导航';
		} else {

			$data['legend'] = '添加分类导航';
		}

		$query = $this->db->from('main')->order_by('order','desc')->get();
        $sidebar_names = $query->result();

		$this->sidebar_content = '<div class="box">';
		foreach($sidebar_names as $sidebar_name)
		{
			$href = site_url("main/index/".$sidebar_name->id."");
			$href_delete = site_url("main/delete_by_id/".$sidebar_name->id."");
			$this->sidebar_content .=  '<div class="quiet">'.$sidebar_name->name.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$href.'">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$href_delete.'" onclick="return confirm(\'确定要删除吗？\')">删除</a></div>';
		}
		$this->sidebar_content .= '</div>';

		$this->load->model('nav_model');
		$this->load->model('ion_auth_model');

		$data['title'] = '分类导航';
		$data['include'] = 'main_view';// include file here
		$data['sidebar_title'] = '边栏名';
		$data['sidebar_content'] = $this->sidebar_content;
		$data['nav_view'] = $this->nav_model->get_nav('分类导航');
		$data['login_info'] = $this->ion_auth_model->get_user_info();

		$defaule_order = '<select name="order"';
		for($i = 0;$i <= 100;$i++)
		{
			if($i == $this->input->post('order'))
				$defaule_order .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
			else if($i == $data['order'])
				$defaule_order .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
			else if($i == 10)
				$defaule_order .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
			else
				$defaule_order .= '<option value="'.$i.'">'.$i.'</option>';
		}
		$defaule_order .= '</select>';

		$data['order'] = $defaule_order;


		$config = array(
               array(
                     'field'   => 'name',
                     'label'   => '内容分类名',
                     'rules'   => 'trim|required|xss_clean|callback_contentname_check|callback_unique_check'
					),

            );
		if($status == 'edit')
		{
			$config = array(
               array(
                     'field'   => 'name',
                     'label'   => '内容分类名',
                     'rules'   => 'trim|required|xss_clean|callback_contentname_check'
					),

            );

		}

		$this->form_validation->set_rules($config);
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

		//$this->form_validation->set_rules('password', 'Password', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('global_view',$data);
		}
		else
		{
			if($status == 'edit') {
				$this->db->where('id', $id);
				$this->db->update('main', $_POST);
			}else
			{
				$this->db->insert('main', $_POST);
			}
			// clear cache
			$this->load->model('clear_cache_model');
			$this->clear_cache_model->clear_cache();
			redirect('main/index','refresh');

		}
	}

	function delete_by_id($id='')
	{
		if($id=='')
			return;
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login','refresh');
		}

		$id = intval($id);

		$query = $this->db->from('main')->where('id',$id)->limit(1)->get();
		if($query->row())
		{
			$query_main_url = $this->db->from('main_url')->where('type',$id)->limit(1)->get();
			{
				if($query_main_url->row())
				{
					;
				}else
				{
					$this->db->where('id', $id);
					$this->db->delete('main');
				}
			}

		}
		redirect('main/index','refresh');

	}

	function contentname_check($str)
	{
		$check_array = array('test','1234');
		if (in_array(strtolower($str),$check_array))
		{
			$this->form_validation->set_message('contentname_check', '%s不能为'.$str);
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	function unique_check($str)
	{
		$query = $this->db->select('name')->from('main')->where('name',$str)->limit(1)->get();

		if ($query->row()) {
 			$this->form_validation->set_message('unique_check', '%s已存在'.$str);
           return false;
        } else {
            return true;
        }
	}


}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */